package cn.edu.sctu.sctu_lysd.presenter;

import android.content.Context;
import android.widget.EditText;

import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.RegisterData;
import cn.edu.sctu.sctu_lysd.biz.RegisterBiz;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.L;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-06.
 */

public class RegisterPresenter implements RegisterBiz.Presenter {

    private Context mContext;
    private RegisterBiz.View view;

    private NetService service;

    private Call<JsonBaseBeen<RegisterData>> registerCall;

    public final static int CODE__EXISTS = 40000501;//用户已存在
    public final static int CODE__SUCCESS = 200;//注册成功
    public final static int CODE__PHONE_CODE_ERROR = 40000500;//验证码错误
    public final static int CODE__NET_ERROR = -1;//注册成功

    public RegisterPresenter(Context mContext, RegisterBiz.View view) {
        this.mContext = mContext;
        this.view = view;
    }

    @Override
    public void start() {

        RetrofitHelper retrofitHelper = RetrofitHelper.getInstance();
        service = (NetService) retrofitHelper.getService(NetService.class);

    }

    @Override
    public void register(String phoneNumber, final String code, String password, String confirmPassword) {

        if (password.equals(confirmPassword)){

            registerCall = service.register(phoneNumber, password, code);

            registerCall.enqueue(new Callback<JsonBaseBeen<RegisterData>>() {
                @Override
                public void onResponse(Call<JsonBaseBeen<RegisterData>> call, Response<JsonBaseBeen<RegisterData>> response) {

                    L.e("onResponse",response.body().getMeta().getCode() + "");

                    switch (response.body().getMeta().getCode()){

                        case CODE__SUCCESS:

                            view.registerSuccess();

                            break;
                        case CODE__EXISTS:

                            view.registerFailed(CODE__EXISTS);

                            break;
                        case CODE__PHONE_CODE_ERROR:

                            view.registerFailed(CODE__EXISTS);

                            break;
                        default:

                            break;
                    }

                }

                @Override
                public void onFailure(Call<JsonBaseBeen<RegisterData>> call, Throwable t) {

                    view.registerFailed(CODE__NET_ERROR);

                }
            });

        }

    }

    @Override
    public void cancel() {

//        if (registerCall.isCanceled())

        if (registerCall != null)

        registerCall.cancel();

    }

    @Override
    public void getCode(String phoneNumber) {

        Call<Object> code = service.getCode(phoneNumber);

        code.enqueue(new Callback<Object>() {


            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                L.e("onResponse", "onResponse");
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {

                L.e("onResponse", "onFailure");
                L.e("onResponse", t.getMessage());
                t.printStackTrace();
            }
        });
    }

    @Override
    public String getInputSting(EditText view) {
        return view.getText().toString();
    }
}
